//
//  DMHomeVC.m
//  DistributionMall
//
//  Created by JHnoplan on 2018/10/14.
//  Copyright © 2018年 JHnoplan. All rights reserved.
//

#import "DMHomeVC.h"
#import "DMHomeCell.h"
#import "SDCycleScrollView.h"
#import "DMGoodsDetailVC.h"

#import "ZYBannerView.h"
//model
#import "KStoreListModel.h"
#import "KStoreBannersModel.h"

//request
#import "KStoreRequest.h"

//other
#import <MJRefresh/MJRefresh.h>

//比例宏
#define KRealValue(value) ((value)/375.0f*[UIScreen mainScreen].bounds.size.width)

@interface DMHomeVC ()<UITableViewDelegate,UITableViewDataSource,ZYBannerViewDelegate,ZYBannerViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

//轮播图
@property (nonatomic,strong) ZYBannerView *bannerView;

@property (nonatomic, strong) NSMutableArray *dataArray;

//banner的内容
@property (nonatomic,strong) NSArray * bannerArr;
@property (nonatomic,assign) NSInteger page;
@end

@implementation DMHomeVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self startInit];
}

-(void)startInit {
    self.navigationItem.title = @"商城";
    _dataArray = [NSMutableArray new];
    self.page = 1;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self setTableHeaderView];
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"DMHomeCell" bundle:nil] forCellReuseIdentifier:@"DMHomeCell"];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadmore)];
    [self.tableView.mj_header beginRefreshing];
    [self getbanners];
}

#pragma mark ----network----
//刷新
-(void)refresh{
    
    KStoreRequest * request = [[KStoreRequest alloc]initStoreListWithName:nil order:nil orderType:@"all" page:@"1" subCateId:nil type:@"2"];
    
    
    [JLProgressHUDManager showLoadingHud];
    MJWeakSelf
    [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        
        weakSelf.page = 1;
        [JLProgressHUDManager hideHud];
        
        KStoreListBaseModel * listModel = [KStoreListBaseModel modelWithJSON:baseModel.data];
        [weakSelf.dataArray removeAllObjects];
        [weakSelf.dataArray addObjectsFromArray:listModel.saleEntityViews];
        
        [weakSelf.tableView reloadData];
        
        [weakSelf.tableView.mj_header endRefreshing];
        
        
    } failure:^(KBaseModel *errorModel) {
        
        [JLProgressHUDManager showErrorHud:errorModel.msg];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}

//下一页
-(void)loadmore{
    self.page++;
    KStoreRequest * request = [[KStoreRequest alloc]initStoreListWithName:@"" order:@"" orderType:@"all" page:@(self.page).stringValue subCateId:nil type:@"2"];
    
    
    [JLProgressHUDManager showLoadingHud];
    MJWeakSelf
    [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        
        [JLProgressHUDManager hideHud];
        
        KStoreListBaseModel * listModel = [KStoreListBaseModel modelWithJSON:baseModel.data];
        
        [weakSelf.dataArray addObjectsFromArray:listModel.saleEntityViews];
        
        [weakSelf.tableView reloadData];
        
        [weakSelf.tableView.mj_footer endRefreshing];
        
        
    } failure:^(KBaseModel *errorModel) {
        weakSelf.page--;
        [JLProgressHUDManager showErrorHud:errorModel.msg];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}



//获取轮播图
-(void)getbanners{
    
    KStoreRequest * request = [[KStoreRequest alloc]initStoreBanners];
    MJWeakSelf
    [request newStartRequestWithCompletion:^(KBaseModel *baseModel) {
        
        KStoreBannersBaseModel * listModel = [KStoreBannersBaseModel modelWithJSON:baseModel.data];
        
        weakSelf.bannerArr =[NSArray arrayWithArray:listModel.bannerViews];
        [weakSelf.bannerView reloadData];
        
        
    } failure:^(KBaseModel *errorModel) {
        
        [JLProgressHUDManager showErrorHud:errorModel.msg];

    }];
    
}



#pragma mark ---tableView

//设置轮播图
-(void)setTableHeaderView{
    
    UIView * bannerView = [[UIView alloc]init];
    
    [bannerView addSubview:self.bannerView];
    
    [self.bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bannerView);
        make.left.right.equalTo(bannerView);
        make.height.equalTo(@KRealValue(200));
    }];
    
    [bannerView layoutIfNeeded];
    bannerView.height = CGRectGetMaxY(self.bannerView.frame);
    self.tableView.tableHeaderView = bannerView;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
//    return 3;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 172;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DMHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DMHomeCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"DMHomeCell" owner:self options:nil].firstObject;
    }
    KStoreListModel * model = self.dataArray[indexPath.row];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.mainImageUrl] placeholderImage:[UIImage imageNamed:@"robotImage"]];
    cell.titleLabel.text = model.name;
    cell.priceLabel.text = [NSString stringWithFormat:@"¥%@",model.salePrice];
    cell.saleCountLabel.text = [NSString stringWithFormat:@"销量%@",model.saleQty];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DMGoodsDetailVC *vc = [DMGoodsDetailVC new];
    KStoreListModel * model = self.dataArray[indexPath.row];
    vc.key =model.id;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark ----banner---


// 返回Banner需要显示Item(View)的个数
- (NSInteger)numberOfItemsInBanner:(ZYBannerView *)banner
{
    return self.bannerArr.count;
}

// 返回Banner在不同的index所要显示的View
- (UIView *)banner:(ZYBannerView *)banner viewForItemAtIndex:(NSInteger)index
{
    KStoreBannersModel * model = self.bannerArr[index];
    UIImageView *imageView = [[UIImageView alloc]init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:model.imageUrl] placeholderImage:nil];
    return imageView;
}

-(void)banner:(ZYBannerView *)banner didSelectItemAtIndex:(NSInteger)index{
    
    
    
}


//轮播图
-(ZYBannerView *)bannerView{
    if (!_bannerView) {
        _bannerView = [[ZYBannerView alloc] initWithFrame:CGRectZero];
        _bannerView.delegate = self;
        _bannerView.dataSource = self;
        _bannerView.autoScroll = YES;
        _bannerView.shouldLoop = YES;
        _bannerView.pageControl.currentPageIndicatorTintColor = HEX_RGB(0x3197FA);
        _bannerView.pageControl.pageIndicatorTintColor = HEX_RGB(0xD8D8D8);
        [self.view addSubview:_bannerView];
    }
    return _bannerView;
}



@end

